package com.comet.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.comet.domain.Order;
import com.comet.mapper.OrderMapper;
import com.comet.service.OrderService;
import com.comet.service.CourseService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    CourseService courseService;

    @Override
    public Order findOrderById(int orderId) {
        return orderMapper.findOrderById(orderId);
    }

    @Override
    @SentinelResource(value = "sentinelFlowLimit")
    public Object sentinelFlowLimit(String query) {
        return new HashMap<String, Object>().put("流控测试","YES");
    }

    @Override
    @GlobalTransactional
    public Object createOrder(Order order) {

        // 插入订单
        int effectLine = orderMapper.createOrder(order);

        // 调用库存服务扣减库存
        courseService.deductStockBySkuId(100100100, 1);

        // 报错了，回滚事务
        // int a = 1 / 0;

        return order;
    }
}
